package cn.zyjblogs.nio.zerocopy;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

public class NewIOClient {
    public static void main(String[] args) {
        String fileName = "C++书单.md";
        try(SocketChannel socketChannel = SocketChannel.open();
            //得到一个文件channel
            FileChannel channel = new FileInputStream(fileName).getChannel()) {
            socketChannel.connect(new InetSocketAddress("localhost",7001));
            //准备发送
            long startTime = System.currentTimeMillis();
            //linux下一个transferTo 方法可以完成发送
            //在windows下一次调用transferTo只能发送8M，就需要分段传输文件，而且要主要
            // 传输时的位置
            //transferTo 底层使用到零拷贝
            long transferCount = channel.transferTo(0,channel.size(),socketChannel);
            System.out.println("发送的总得到字节数 "+ transferCount+" ,耗时： "+(System.currentTimeMillis() - startTime));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
